﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UlsStudio.DesktopApp.Properties;

namespace UlsStudio.DesktopApp
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        #region Event Handlers
        private void chkEnable_CheckedChanged(object sender, EventArgs e)
        {
            EnabledControls();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            // Load settings
            UpdateControlsFromSettings();

            // Set GUI
            EnabledControls();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            // Save settings
            Settings.Default.EnableColouring = chkEnable.Checked;

            Settings.Default.LineColourExceptionBack = unexpectedLineStylePicker.LineBackColour;
            Settings.Default.LineColourExceptionFore = unexpectedLineStylePicker.LineForeColour;

            Settings.Default.LineColourCriticalBack = criticalLineStylePicker.LineBackColour;
            Settings.Default.LineColourCriticalFore = criticalLineStylePicker.LineForeColour;

            Settings.Default.LineColourExceptionBack = exceptionsLineStylePicker.LineBackColour;
            Settings.Default.LineColourExceptionFore = exceptionsLineStylePicker.LineForeColour;

            // Set all as "OK"
            AcceptSettingsAndClose();
        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            bool cont = MessageBox.Show("Are you sure you want to reset these settings to thier defaults?",
                "Reset Settings to Default",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes;
            if (cont)
            {
                Settings.Default.Reset();
                UpdateControlsFromSettings();
                AcceptSettingsAndClose();
            }
        }

        #endregion

        void EnabledControls()
        {
            pnlColours.Enabled = chkEnable.Checked;
        }

        void UpdateControlsFromSettings()
        {
            chkEnable.Checked = Settings.Default.EnableColouring;

            unexpectedLineStylePicker.LineBackColour = Settings.Default.LineColourUnexpectedBack;
            unexpectedLineStylePicker.LineForeColour = Settings.Default.LineColourUnexpectedFore;

            criticalLineStylePicker.LineBackColour = Settings.Default.LineColourCriticalBack;
            criticalLineStylePicker.LineForeColour = Settings.Default.LineColourCriticalFore;

            exceptionsLineStylePicker.LineBackColour = Settings.Default.LineColourExceptionBack;
            exceptionsLineStylePicker.LineForeColour = Settings.Default.LineColourExceptionFore;
        }


        void AcceptSettingsAndClose()
        {
            SettingsCache.Refresh();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        
    }
}
